package com.googlecode.bushel.obr.filter;

import com.googlecode.bushel.obr.xml.RequirementFilter;

public class CompareFilter extends RequirementFilter {

    public enum Operator {
        EQUALS, LOWER_THAN, LOWER_OR_EQUAL, GREATER_THAN, GREATER_OR_EQUAL;

        @Override
        public String toString() {
            switch (this) {
            case EQUALS:
                return "=";
            case GREATER_THAN:
                return ">";
            case GREATER_OR_EQUAL:
                return ">=";
            case LOWER_THAN:
                return "<";
            case LOWER_OR_EQUAL:
                return "<=";
            default:
                break;
            }
            return super.toString();
        }
    }

    private Operator operator;

    private final String rightValue;

    private final String leftValue;

    public CompareFilter(String leftValue, Operator operator, String rightValue) {
        this.leftValue = leftValue;
        this.rightValue = rightValue;
        this.operator = operator;
    }

    public String getLeftValue() {
        return leftValue;
    }

    public Operator getOperator() {
        return operator;
    }

    public String getRightValue() {
        return rightValue;
    }

    @Override
    public void append(StringBuilder builder) {
        builder.append("(");
        builder.append(leftValue);
        builder.append(operator.toString());
        builder.append(rightValue);
        builder.append(")");
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((leftValue == null) ? 0 : leftValue.hashCode());
        result = prime * result + ((operator == null) ? 0 : operator.hashCode());
        result = prime * result + ((rightValue == null) ? 0 : rightValue.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (!(obj instanceof CompareFilter)) {
            return false;
        }
        CompareFilter other = (CompareFilter) obj;
        if (leftValue == null) {
            if (other.leftValue != null) {
                return false;
            }
        } else if (!leftValue.equals(other.leftValue)) {
            return false;
        }
        if (operator == null) {
            if (other.operator != null) {
                return false;
            }
        } else if (!operator.equals(other.operator)) {
            return false;
        }
        if (rightValue == null) {
            if (other.rightValue != null) {
                return false;
            }
        } else if (!rightValue.equals(other.rightValue)) {
            return false;
        }
        return true;
    }

}
